﻿using AutoMapper;
using AutoMapper.Mappers;
using LianChuDemo.IDal.BaseInfoManagerDal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WenDangOne.DbModel.BaseInfoManagerDal;
using WenDangOne.ViewModel.BaseInfoManagerView;

namespace LianChuDemo.Bll.BaseInfoManagerBll
{
    public class ModuleDbBll
    {
        //接口
        private IModuleDbDal _iModuleDbDal;
        private IMapper _mapper;

        //注入
        public ModuleDbBll(IModuleDbDal iModuleDbDal,IMapper mapper)
        {
            _iModuleDbDal = iModuleDbDal;
            _mapper = mapper;
        }
        public List<ModuleDb> GetModuleDbsAll()
        {
            return _iModuleDbDal.GetModuleDbsAll();
        }
        public List<ModuleDbView> GetModuleDbViewsAll()
        {
            List<ModuleDb> moduleDbs = GetModuleDbsAll();
            List<ModuleDbView> viewsAll = new List<ModuleDbView>();
            List<ModuleDb> oneModel = moduleDbs.Where(c => c.module_level == 1).ToList();
            foreach(var item in oneModel)
            {
                viewsAll.Add(IniViewModel(item, moduleDbs));
            }
            return viewsAll;
        }
        public ModuleDbView IniViewModel(ModuleDb db, List<ModuleDb> moduleDbs)
        {
            ModuleDbView view = _mapper.Map<ModuleDbView>(db);
            List<ModuleDb> sonList = moduleDbs.Where(p => p.parent_code == view.module_code).ToList();
            if(sonList!=null && sonList.Count>0)
            {
                view.SonModules = new List<ModuleDbView>();
                foreach(var s in sonList)
                {
                    ModuleDbView son = IniViewModel(s, moduleDbs);
                    view.SonModules.Add(son);
                }
            }
            return view;
        }

        public bool VModuleCode(string moduleCode)
        {
            if (_iModuleDbDal.GetModuleDbByModuleCode(moduleCode) == null) return true;
            return false;
        }

        public bool ModuleAdd(ModuleDb moduleDb)
        {
            return _iModuleDbDal.ModuleAdd(moduleDb);
        }
        //反填
        public ModuleDbView GetModuleDbViewByCode(string code)
        {
            return _iModuleDbDal.GetModuleDbViewByCode(code);
        }
        public ModuleDbView GetModuleDbViewByCodeSonSql(string moduleCode)
        {
            return _iModuleDbDal.GetModuleDbViewByCodeSonSql(moduleCode);
        }
        public bool ModuleUpdate(ModuleDb moduleDb)
        {
            return _iModuleDbDal.ModuleUpdate(moduleDb);
        }

        //根据用户名获取用户下的所有模块
        public List<ModuleDb> GetModuleDbsByUserName(string userName)
        {
            return _iModuleDbDal.GetModuleDbsByUserName(userName);
        }
    }
}
